// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockURControlClientNetworkConnection.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Hardware.UniversalRobots
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;

    using Microsoft.Robotics.Hardware.UniversalRobots;

    /// <summary>
    /// Mock connection to URControl server.
    /// </summary>
    public class MockURControlClientNetworkConnection : IURControlClientNetworkConnection, IDisposable
    {
        /// <summary>
        /// Mock stream.
        /// </summary>
        private MockNetworkStream stream;

        /// <summary>
        /// Initializes a new instance of the <see cref="MockURControlClientNetworkConnection"/> class.
        /// </summary>
        /// <param name="packetToSend">Packet to be sent when the network stream is read.</param>
        /// <param name="delayBetweenPacketsInMs">Approximate delay between when a ReadAsync is called and when a success result is returned.</param>
        public MockURControlClientNetworkConnection(byte[] packetToSend, int delayBetweenPacketsInMs)
        {
            this.stream = new MockNetworkStream(packetToSend, delayBetweenPacketsInMs);
        }

        /// <summary>
        /// Gets a value indicating whether or not connection is made to URServer.
        /// </summary>
        public bool Connected
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets the last command sent to the mock network packet
        /// </summary>
        public byte[] LastCommandReceived
        {
            get
            {
                return this.stream.LastReceivedPacket;
            }
        }

        /// <summary>
        /// Releases resources.
        /// </summary>
        public void Close()
        {
            this.Dispose();
        }

        /// <summary>
        /// Dispose of resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);

            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Mock connection (always returns success immediately)
        /// </summary>
        /// <param name="hostname">The parameter is not used.</param>
        /// <param name="port">The parameter is not used.</param>
        /// <returns>A task.</returns>
        public Task ConnectAsync(string hostname, int port)
        {
            Task task = new Task(() => { return; });
            task.Start();
            return task;
        }

        /// <summary>
        /// Returns mock network stream.
        /// </summary>
        /// <returns> A mock network stream.</returns>
        public Stream GetStream()
        {
            return this.stream;
        }

        /// <summary>
        /// Dispose of resources.
        /// </summary>
        /// <param name="disposing">True if <c>Dipsose()</c> was called.</param>
        private void Dispose(bool disposing)
        {
            // only dispose of managed resources if Dispose() was called.
            if (disposing)
            {
                this.stream.Dispose();
            }
        }
    }
}
